package com.sangfor.ssl.common;

import android.content.Context;
import com.sangfor.bugreport.logger.Log;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

/* loaded from: classes2.dex */
public class PublicKeyEntery {
    private static final String TAG = "PublicKeyEntery";
    private static volatile String sCryptoKey;
    private static volatile String sPublicKey;

    /* JADX WARN: Not initialized variable reg: 2, insn: 0x0041: MOVE  (r0 I:??[OBJECT, ARRAY]) = (r2 I:??[OBJECT, ARRAY]), block:B:24:0x0041 */
    /* JADX WARN: Removed duplicated region for block: B:34:0x0044 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private static java.lang.String getPublicKey(java.io.File r3, java.lang.String r4, java.lang.String r5) {
        /*
            r0 = 0
            java.lang.String r1 = "JKS"
            java.security.KeyStore r1 = java.security.KeyStore.getInstance(r1)     // Catch: java.lang.Throwable -> L2e java.lang.Exception -> L30
            java.io.FileInputStream r2 = new java.io.FileInputStream     // Catch: java.lang.Throwable -> L2e java.lang.Exception -> L30
            r2.<init>(r3)     // Catch: java.lang.Throwable -> L2e java.lang.Exception -> L30
            char[] r3 = r4.toCharArray()     // Catch: java.lang.Exception -> L2c java.lang.Throwable -> L40
            r1.load(r2, r3)     // Catch: java.lang.Exception -> L2c java.lang.Throwable -> L40
            java.security.cert.Certificate r3 = r1.getCertificate(r5)     // Catch: java.lang.Exception -> L2c java.lang.Throwable -> L40
            java.security.PublicKey r3 = r3.getPublicKey()     // Catch: java.lang.Exception -> L2c java.lang.Throwable -> L40
            byte[] r3 = r3.getEncoded()     // Catch: java.lang.Exception -> L2c java.lang.Throwable -> L40
            java.lang.String r3 = getFormattedText(r3)     // Catch: java.lang.Exception -> L2c java.lang.Throwable -> L40
            r2.close()     // Catch: java.io.IOException -> L27
            goto L2b
        L27:
            r4 = move-exception
            r4.printStackTrace()
        L2b:
            return r3
        L2c:
            r3 = move-exception
            goto L32
        L2e:
            r3 = move-exception
            goto L42
        L30:
            r3 = move-exception
            r2 = r0
        L32:
            r3.printStackTrace()     // Catch: java.lang.Throwable -> L40
            if (r2 == 0) goto L3f
            r2.close()     // Catch: java.io.IOException -> L3b
            goto L3f
        L3b:
            r3 = move-exception
            r3.printStackTrace()
        L3f:
            return r0
        L40:
            r3 = move-exception
            r0 = r2
        L42:
            if (r0 == 0) goto L4c
            r0.close()     // Catch: java.io.IOException -> L48
            goto L4c
        L48:
            r4 = move-exception
            r4.printStackTrace()
        L4c:
            throw r3
        */
        throw new UnsupportedOperationException("Method not decompiled: com.sangfor.ssl.common.PublicKeyEntery.getPublicKey(java.io.File, java.lang.String, java.lang.String):java.lang.String");
    }

    private static String getFormattedText(byte[] bArr) {
        char[] cArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        int length = bArr.length;
        StringBuilder sb = new StringBuilder(length * 2);
        for (int i = 0; i < length; i++) {
            sb.append(cArr[(bArr[i] >> 4) & 15]);
            sb.append(cArr[bArr[i] & 15]);
        }
        return sb.toString();
    }

    public static String getPublicKey(String str, String str2, String str3) {
        File file = new File(str);
        if (!file.exists()) {
            System.err.println(str);
            return "";
        }
        return getPublicKey(file, str2, str3);
    }

    public static String getCryptKey(String str, String str2, String str3) {
        try {
            String publicKey = getPublicKey(str, str2, str3);
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(publicKey.getBytes());
            return getFormattedText(messageDigest.digest()).substring(0, 16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getPublicKey(Context context) {
        if (sPublicKey == null) {
            synchronized (PublicKeyEntery.class) {
                if (sPublicKey == null) {
                    sPublicKey = "";
                    try {
                        sPublicKey = getFormattedText(((X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(context.getPackageManager().getPackageInfo(context.getPackageName(), 64).signatures[0].toByteArray()))).getPublicKey().getEncoded());
                    } catch (Exception e) {
                        Log.error(TAG, "getPublicKey failed", e);
                    }
                }
            }
        }
        return sPublicKey;
    }

    public static String getCryptKey(Context context) {
        if (sCryptoKey == null) {
            synchronized (PublicKeyEntery.class) {
                if (sCryptoKey == null) {
                    sCryptoKey = "";
                    try {
                        String publicKey = getPublicKey(context);
                        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                        messageDigest.update(publicKey.getBytes());
                        sCryptoKey = getFormattedText(messageDigest.digest()).substring(0, 16);
                    } catch (NoSuchAlgorithmException e) {
                        Log.error(TAG, "getCryptKey failed", e);
                    }
                }
            }
        }
        return sCryptoKey;
    }
}
